
Page({
  data: {
    inputPassword: false,
    account: '',
    password: '',
    hidden: true,
    name_focus:''
  },
  //页面加载时
  onLoad: function () {
    var user = wx.getStorageSync('account');
    var password = wx.getStorageSync('password');
    wx.removeStorageSync('dayShopIdStr')
    if (user && password){
         this.setData({
           account:user,
           password: password
         })
    }
  },
  bindAccountInput(e) {
    var upAccount = e.detail.value.toUpperCase();
    console.log(upAccount)
    this.setData({
      account: upAccount , //用户名
    })
  },
  bindPasswordInput(e) {
    var upPassword = e.detail.value.toUpperCase();
    this.setData({
      password: upPassword //密码
    })
  },
  //密码框获取焦点时
  pwdFocus(e){
    if (this.data.account.length == 0) {
      this.setData({
        infoMess: '用户名不能为空！',
      })
    }
  },
  accountFocus(e){
    this.setData({
      infoMess: '',
      passWd:""
    })
  },

  //点击登录
  bindIdentity:function(e) {
    var that = this
    console.log(e)
    var account = this.data.account;
    var password = this.data.password;
    wx.setStorageSync('account', account);//储存到本地
    wx.setStorageSync('password', password);//储存到本地
    if (account.length == 0 || password.length == 0) {
      this.setData({
        passWd: '温馨提示：用户名和密码不能为空！',
      })
      return;
    } 
    // 显示加载中
    wx.showLoading({
      title: '加载中',
    })
    //发送请求
    wx.request({
      url:'http://ctms.netideas.com.hk/Main/api/login',
      data:{
        UserID: account,
        Password: password
      },
      method: 'POST',
      header:{
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        wx.hideLoading();
        var result = res.data
        console.log(result.Status)
        if (result.Status == false) {
          wx.showModal({
            title: '提示',
            content: '用户名或密码错误，请重新输入！',
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
          return;
        }else{
          //跳转页面
          wx.setStorageSync('Token', result.Token)//储存到本地
          wx.switchTab({
            url: '../Home/home',
          })
        }
        
      },
      fail(e){
        wx.hideLoading();
        wx.showModal({
          title: '提示',
          content: '请求超时，请稍后重试！',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      },
      complete(e){
        console.log(e)
      }
    })
     
  }

})
